package com.jason.kotlin.activityresult

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.jason.kotlin.constant.Constants

/**
 * Description:SecondActivity
 *
 * @author chenbaoyang
 * @create 2021/1/14 13:52
 */
class SecondActivity : AppCompatActivity() {

    private lateinit var container: LinearLayout

    private lateinit var button: Button

    private lateinit var textView : TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //创建容器view
        container = LinearLayout(this)
        val layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        ).apply {
            gravity = Gravity.CENTER
        }
        container.layoutParams = layoutParams
        container.orientation = LinearLayout.VERTICAL

        //创建点击按钮
        button = Button(this)
        button.apply {
            text = "回传数据"
            setOnClickListener {
                onClick()
            }
        }

        textView = TextView(this)

        //添加到容器中
        container.addView(button)
        container.addView(textView)

        setContentView(container)

        val name = intent.getStringExtra("name")
        textView.text = "接收到的数据为：$name"
    }

    private fun onClick() {
        val intent = Intent().apply {
            putExtra("result", "Hello，我是回传的数据！")
        }
        setResult(Activity.RESULT_OK, intent)
        finish()
    }
}